<template>
  <div class="el-mr-scanningReturn">
    <TableTitle>
      <slot>
        <p>
          扫码操作
        </p>
      </slot>
    </TableTitle>
    <TopSearch ref="topSearch"> 
      <slot>
       <li v-if="isReturnReason">
          退回原因
        </li>
        <li v-if="isReturnReason">
          <el-input
            size="mini"
            v-model="returnReason"
            placeholder="请输入内容"
            :disabled="isDisabled"
          ></el-input>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="scanning" >扫码退回</el-button>
        </li>
        <li>
          <p>扫码人工号：{{smPerson}}</p>
        </li>
      </slot>
    </TopSearch>
    <TableTitle>
      <slot>
        <p>
          查询条件
        </p>
      </slot>
    </TableTitle>
    <MidSearch ref="midSearch"></MidSearch>
    <TableTitle>
      <slot>
        <p>
          已退回物料列表
        </p>
      </slot>
    </TableTitle>
    <Table @materialId="materialId"></Table>
  </div>
</template>
<script>
import { changeRetrunType,mrMhAll,changeRetrunCheck } from "@/utils/api.js";
import TopSearch from "../../../../components/TopSearch.vue";
import Table from "./components/Tables.vue"
import MidSearch from "./components/MidSearch.vue"
import TableTitle from "../../../../components/TableTitle.vue"
import request from "@/utils/request";


export default {
  name: "ScanningReturn",
  components: {
    TopSearch,
    Table,
    MidSearch,
    TableTitle
  },
  data() {
    return {
      resData:'',
      totalCount:'',
      idList:[],
      pageSize:20,
      pageNumber:1,
      tiaoma:'',
      returnReason:'101扫码退回',
      // 控制退回原因隐藏
      isReturnReason:true,
      // 控制退回原因是否可输入
      isDisabled:false,
      smPerson:''
    };
  },
  methods: {
    // 扫码退回按钮
    scanning(){
      if(this.smPerson == ''){
        request.post("/WmsDeliverPlan/searchWorkerCode",{"smPerson":this.tiaoma}).then((res)=>{
          console.log('res',res)
          if(res.data.retCode=='201'){
            this.$Alert({
              title: "失败",
              message: res.data.message,
              type: "error",
          });
          }else{
            this.$Alert({
              title: "成功",
              message: res.data.message,
              type: "success",
          })
            this.smPerson = this.tiaoma
            this.$options.methods.next.bind(this)()
          }
        });
      }else if(this.tiaoma=="9005"){//扫码退回

      }else{
        let barcode = ''
        var barcodeType = "",bzjCodeType="";
        if(this.tiaoma.includes('{')){
            var tiaomaObj = JSON.parse(this.tiaoma);
            if(tiaomaObj.hasOwnProperty("CPHGZ")){//成品
              barcode = tiaomaObj.CPHGZ;
              barcodeType = "CP";
            }else{//标件
              barcode = tiaomaObj.CKCODE;
              barcodeType = "BJ";
              bzjCodeType = tiaomaObj.BARCODETYPE;//BZJCK  BZJCKAO
            }
        }else{
          barcode = this.tiaoma;
          barcodeType = "LJ";
        }
        changeRetrunCheck({
            barcode:barcode,
            userCode:this.smPerson,
        }).then((res) => {
          console.log("res==========",res);
          if(res.data.retCode=='200'){
            this.isReturnReason = true
              changeRetrunType({
              barcode:barcode,
              returnReason:this.returnReason,
              userCode:this.smPerson,
              barcodeType:barcodeType,
              bzjCodeType:bzjCodeType
              }).then((res) =>{
                if(res.data.retCode=='200'){
                  // alert("扫码退回成功");
                  this.$Alert({
                    title: "成功",
                    message: '扫码退回成功！',
                    type: "success",
                  });
                  // 退回成功清空原因
                //  this.returnReason = ''
                  // 让退回原因input变为不可输入状态
               //   this.isDisabled = true
                  // 扫码退回成功查当天数据，然后聚焦扫码焦点
                  this.$refs.midSearch.scanningSearch()
                  this.$refs.topSearch.openScan()
                }else{
                  this.$Alert({
                    title: "错误",
                    message: '扫码退回失败！'+res.data.message,
                    type: "error",
                  });
                }
              })
          }else{
            this.$Alert({
                    title: "错误",
                    message: '未找到移交条码数据！',
                    type: "error",
                  });
          }
        });
      }
      
    },
    materialId(value){
      // 接收Table勾选的信息info
      this.idList = value
      
    }
  },
  mounted() {
      // 接收TopSearch的条码 
      this.$bus.$on('saoma',(e)=>{
        this.tiaoma = e
        // console.log('条码',this.tiaoma)
      })
  },
};
</script>
<style scoped>
.el-input {
  width: 450px;
}
</style>